<?php

$matchId = $_GET["matchId"];
@ $matchOptaId = $_GET["matchOptaId"];
@ $uId = $_GET["id"];
$userIp = $_SERVER["REMOTE_ADDR"];
$userMac = $_GET["mac"];
$t = $_GET["t"];

$config = require 'conf/config.ini.php';      //加载公用config.ini.php
$conn = mysql_connect($config["DB_HOST"], $config["DB_USER"], $config["DB_PWD"]);
mysql_select_db($config["DB_NAME"], $conn);
mysql_query("SET NAMES utf8");

if ($uId == "null") {
	$uId = null;
}

//记录观看比赛者信息
if ($t == "1") {
	$sql_watch = "insert into live_watch (matchId,matchOptaId,userId,userIp,userMac,userInTime) values ('" . $matchId . "', '" . $matchOptaId . "', '" . $uId . "', '" . $userIp . "', '" . $userMac . "', now())";
	mysql_query("SET NAMES gbk");
	mysql_query($sql_watch, $conn);
	//当为注册用户时
	if($uId != null){
		//添加关注话题，一人一天一场比赛添加一次
		$sql_tpoic = "select count(w.userId) wCount from live_watch w where w.updateDate >= CURDATE() and w.updateDate < date_add(CURDATE(),INTERVAL 1 day)  and w.userId = '". $uId ."' and w.matchId = '". $matchId ."'";
		mysql_query("SET NAMES utf8");
		$result = mysql_query($sql_tpoic, $conn);
		$result = mysql_fetch_array($result);
		if($result[0] == "1"){
			$sql_match = "select CONCAT('#',matchName,' ',homeTeam,'VS',visitTeam,'#')content from live_match where matchId = '".$matchId."'";
			$sql_tpoic = "insert into live_topic (topicTitle, topicContent, userId, userIp)values((" . $sql_match . "),'关注了比赛','" . $uId . "','" . $userIp . "')";
			echo $sql_tpoic;
			mysql_query("SET NAMES utf8");
			mysql_query($sql_tpoic, $conn);
		
		}
	}
} else if ($t == "0") {
	$sql_watch = "update live_watch set userOutTime = now() where (matchId='" . $matchId . "' or matchId = '" . $matchOptaId . "') and userIp='" . $userIp . "'  and userMac='" . $userMac . "' and userOutTime IS NULL";
	mysql_query("SET NAMES gbk");
	mysql_query($sql_watch, $conn);
}
?>